//******************************************************************
#include <windows.h>
#include <WinSock2.h>
#include <iostream>

#include <stdio.h>
#include <io.h>

using namespace std; 

int main(int argc, char * argv[])
{
    int result = 0; 
    //初始化winsock 
	
    WSADATA wsadata; 
    result = WSAStartup(MAKEWORD(2,2), &wsadata); 

    if ( 0 != result ) 
    {
		printf("WSAStartup error %d\n", result);
        return 0; 
    } 
	
    //创建一个地址 
    int serverPort      = 21; 
    char FAR serverIP[] = "172.31.106.100"; //本机ip，不知道就ipconfig 

    SOCKADDR_IN serverAddr; 
    serverAddr.sin_family       = AF_INET; 
    serverAddr.sin_port         = htons( serverPort ); 
    serverAddr.sin_addr.s_addr  = inet_addr( serverIP ); 
	
    int serverAddr_size         = static_cast<int>( sizeof(serverAddr) ); 

    //创建一个socket 
    SOCKET socket_toServer; 
    socket_toServer = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); 
    if ( INVALID_SOCKET == socket_toServer ) 
    { 
		printf("socket_toServer creat failed  %d\n", WSAGetLastError());
        return 0; 
    } 
	
    //连接 
    result = connect( socket_toServer, (sockaddr *)&serverAddr, serverAddr_size ); 
    if ( SOCKET_ERROR == result ) 
    { 
		printf("connect error : %d\n", WSAGetLastError());
        return 0; 
    } 

    char sendbuff[2048]; 
    char recvbuff[2048]; 
	int ret;
	////////////////////////////////////////////////////////////////
	
step_1:
	strcpy(sendbuff, "hello\r\n");
	ret = send(socket_toServer, sendbuff, strlen(sendbuff), 0);
	if ( ret == SOCKET_ERROR ) { 
		cout << "send error " << WSAGetLastError() <<  endl; 
		exit(1);
	}
	ret = recv(socket_toServer, recvbuff, sizeof(recvbuff), 0);
	if (ret == SOCKET_ERROR) {
		printf("recv fail %s! \n", WSAGetLastError());
		exit(1);
	}
	recvbuff[ret] = 0;
	if (strcmp(recvbuff, "good\r\n"))
		goto step_1;
	printf("%s", recvbuff);
	//
	FILE * fp;
	int length;
	size_t total;
	fp = fopen("1.jpg", "rb");
	length = filelength(_fileno(fp));
	printf("the 1.jpg size = %d\n", length);
	
step_2:
	sprintf(sendbuff, "length:%d\r\n", length);
	ret = send(socket_toServer, sendbuff, strlen(sendbuff), 0);
	
	ret = recv(socket_toServer, recvbuff, sizeof(recvbuff), 0);
	recvbuff[ret] = 0;
	if (strncmp(recvbuff, "ok:", 3)) {
		printf("not ok!\n");
		goto step_2;
	}
	printf("%s", recvbuff);
	
	total = length;
	while (1)
	{
		size_t readcnt;
		readcnt = fread(sendbuff, 1, sizeof(sendbuff), fp);
		printf("read %d byte\n", readcnt);
		if (readcnt == 0)
			break;
		if (feof(fp))
			break;
		ret = send(socket_toServer, sendbuff, readcnt, 0);
		Sleep(50);
	}
	fclose(fp);
	

	
    /*
	while( true ) 
    {
        cout << "input send info:" << endl; 
        cin >> sendbuff; 

        int ret = send( socket_toServer, sendbuff, static_cast<int>( strlen(sendbuff) ), 0 ); 
        if ( SOCKET_ERROR == ret ) 
        { 
            cout << "send error " << WSAGetLastError() <<  endl; 
            break; 
        } 

        //处理接受的消息,由于之前没有accept和listen，这里使用recvfrom来接受 
        int nRecv = 0; 
        nRecv = recvfrom( socket_toServer, recvbuff, static_cast<int>( strlen(recvbuff) ), 0, (sockaddr *)&serverAddr, &serverAddr_size ); 
        if ( 0 < nRecv ) 
        { 
            recvbuff[nRecv] = '\0'; 
            cout << "receive : " << recvbuff << endl; 
            cout << "from : " << inet_ntoa( serverAddr.sin_addr ) << endl; 
            cout << " " << endl; 
        } 
    } 
	*/
	
    //清除各种数据和链接 
    closesocket( socket_toServer ); 
    if ( SOCKET_ERROR == WSACleanup() ) 
    { 
        cout << "WSACleanup error " << WSAGetLastError() << endl; 
        return 0; 
    } 
    return 0; 
} 



